--with trace
include qt/bigfixedmath.e
--~ include pause.e

--set_places(3)
function passed(integer i)
  if (i) then
    return "  Pass"
  else
    return "--Fail"
  end if
end function

function  pause(object x = {"\nProblem", 0})
  atom t, d
  t = time()

   puts(1, x[1])
 while ((get_key() = -1) and (t > time())) do
  end while

    return "--Fail"
end function


constant tested = {""
  ,"flatseq"
  ,"textnumber"
  ,"bigfixednumber"
  ,"trim_head"
  ,"trim_tail"
  ,"cmp"
  ,"uadd"
  ,"udiff"
  ,"usub"
  ,"umul"
  ,"udiv"
}


atom delays
integer rid, trigger, test_equal
sequence sets, set, name, nametype, params, ids
object test
delays = 0.5

trigger = 0
procedure run_test()
  integer len_s

  rid = find(name, tested)
  if (rid) then
    rid = ids[rid]
    printf(1, "\nTesting: %s %s(%s)\n", {nametype, name, params})
    puts(1, repeat('-', 79))
    len_s = length(sets)
    for index = 2 to len_s do

      if (trigger = (index - 1)) then
        trace(1)
      end if

      if (index > 2 and (remainder(index, 20) = 2)) then
        --~ pause({"\t[More]", delays})
        --~ clear_screen()
        printf(1, "\nTesting: %s %s(%s)\n", {nametype, name, params})
        --~ puts(1, repeat('-', 79))
      end if
      set = sets[index]
      if (find(',', params)) then
        test = call_func(rid, set[2])
      else
        test = call_func(rid, {set[2]})
      end if
      test_equal = equal(set[1], test)
      printf(1, "\n%s: %d", {passed(test_equal), index-1})
      if (not test_equal) then
        pause("")
      end if
      if ((index != len_s) and remainder(index, 10) = 1) then
        puts(1, 10)
      end if
    end for
    --~ pause({"\n\n[Next]", delays})
    --~ clear_screen()
  end if
end procedure

ids = repeat(0, length(tested))
for index = 2 to length(ids) do
  ids[index] = routine_id(tested[index])
end for

nametype = "type"
name = "flatseq"
params = "object x"
sets = {""
  ,{1, "123,456,789"}
  ,{1, {45, 2, 34, 324, 1234, 34, 'x', 'z', 123.343}}
  ,{0, {0, "x"}}
  ,{0, {0,0,0,0,0, "xyz", 0,0,0}}
  ,{0, {0,0,0,0,0,0,0,0, "xyz"}}

  ,{0, {"xyz", 0}}
  ,{0, 0}
  ,{0, 45}
  ,{0, 1.23}
  ,{0, -1.23}
}
run_test()

name = "textnumber"
params = "object x"
sets = {""
  ,{1, "0"}
  ,{1, "+0"}
  ,{1, "+12345678"}
  ,{1, "+12,345,678"}
  ,{1, "-12,345,678"}

  ,{1, "0.12345678"}
  ,{1, "0.123 456 78"}
  ,{1, ".12345678"}
  ,{1, ".123 456 78"}
  ,{1, "-0.123 456 78"}

  ,{1, "+0.123 456 78"}
  ,{1, "+123,456.123 456 78"}
  ,{0, 0}
  ,{0, 123}
  ,{0, -123}

  ,{0, -123.23}
  ,{0, "123a"}
  ,{0, "$123"}
  ,{0, "12%"}   -- to be added?
  ,{0, {"12", "23"}}

}
run_test()

name = "bigfixednumber"
params = "object x"
sets = {""
  ,{1, {10, "1234"-'0', 0}}
  ,{1, {10, "1234"-'0', 4}}
  ,{1, {128, "123. $abcd", 30}}
  ,{1, {2, "1010001001000101"-'0', 7}}
  ,{0, {10, "1234"-'0'}}

  ,{0, {128, "123. $abcd"}}
  ,{0, {10.1, "1234"-'0', 4}}
  ,{0, {10, "1234"-'0', 4.6}}
  ,{0, {"", "1234"-'0', 4}}
  ,{0, {"asdf", "1234"-'0', 4}}

  ,{0, {10, "1234"-'0', "asdf13"}}

}
run_test()

nametype = "function"
name = "trim_head"
params = "sequence s, atom a"
sets = {""
  ,{"123.000", {"0000123.000", '0'}}
  ,{"1234", {"01234", '0'}}
  ,{"beware of the alpha", {" beware of the alpha", ' '}}
  ,{" beware of me", {"\t beware of me", '\t'}}
  ,{"no trim", {"no trim", 'x'}}
}
run_test()

name = "trim_tail"
params = "sequence s, atom a"
sets = {""
  ,{"0000123", {"0000123.000", '0'}}
  ,{"1234", {"1234.", 'f'}}
  ,{"beware", {"beware. ", ' '}}
  ,{"not sexy.", {"not sexy..\t", '\t'}}
  ,{"no trim", {"no trim.xxxxxxxxxxxxxxx", 'x'}}

  ,{"123.45", {"123.4500", '0'}}
}
run_test()

name = "cmp"
params = "bigfixednumber bn1, bigfixednumber bn2"
sets = {""
  -- 0 equals 0
  ,{0, {text_num("0"), text_num("0")}}
  ,{0, {text_num("0"), text_num("0.0")}}
  ,{0, {text_num("0"), text_num("-0")}}
  ,{0, {text_num("0"), text_num("-0.")}}
  ,{0, {text_num("0"), text_num("+0")}}

  ,{0, {text_num("0"), text_num(".")}}
  ,{0, {text_num("0"), text_num("0000")}}
  ,{0, {text_num("0"), text_num("000,000")}}
  ,{0, {text_num("0"), text_num("00,000.000 000")}}
  ,{0, {text_num("0"), text_num("0 0.0 0")}}
--10

  -- 1 equals 1
  ,{0, {text_num("1"), text_num("+1.0")}}
  ,{0, {text_num("1"), text_num("01")}}
  ,{0, {text_num("1"), text_num("+0,001.000")}}
  ,{0, {text_num("1"), text_num("+0,000 001")}}
  ,{0, {text_num("1"), text_num("1.000 000")}}

  -- 1234 equals 1234
  ,{0, {text_num("1234"), text_num("1234.")}}
  ,{0, {text_num("1234"), text_num("1,234.000 00")}}
  -- 2 > x
  ,{1, {text_num("2"), text_num("1")}}
  ,{1, {text_num("2"), text_num("0")}}
  ,{1, {text_num("2"), text_num("-1")}}
--20

  -- 2 > x
  ,{1, {text_num("2"), text_num("0.1")}}
  ,{1, {text_num("2"), text_num("0.00000 1")}}
  ,{1, {text_num("2"), text_num("1.11")}}
  ,{1, {text_num("2"), text_num("-2.7 1828 1828 45 90 45")}}
  ,{1, {text_num("2"), text_num("-3.14159")}}
  
  -- x < y
  ,{-1, {text_num("-45"), text_num("0")}}
  ,{-1, {text_num("0"), text_num("45")}}
  ,{-1, {text_num("-0.000 001"), text_num("0")}}
  ,{-1, {text_num("-90"), text_num("5")}}
  ,{-1, {text_num("-3.14159"), text_num("2.71828")}}
--30

  -- Double negative test
  ,{1, {text_num("-1"), text_num("-2")}}
  ,{1, {text_num("-1"), text_num("-2.04")}}
}
run_test()

set_base(2)
name = "uadd"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{0}, {{0}, {0}}}
  ,{{1}, {{0}, {1}}}
  ,{{1}, {{1}, {0}}}
  ,{{1,0}, {{1}, {1}}}
  ,{{1,1}, {{1,0}, {1}}}

  ,{{1,0,0}, {{1,1}, {1}}}
  ,{{1,1,0}, {{1,0,1}, {1}}}
  ,{{1,0,0,0}, {{1,0,1}, {1,1}}}
  ,{{1,0,1,0,0}, {{1,0,1,0}, {1,0,1,0}}}
}
run_test()

set_base(3)
name = "uadd"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{2}, {{1}, {1}}}
  ,{{1,0}, {{2}, {1}}}
  ,{{1,0}, {{1}, {2}}}
  ,{{1,1,2}, {{1,1,1}, {1}}}
  ,{{1,0,0,0}, {{2,2,2}, {1}}}

  ,{{1,0,0}, {{1,2}, {1,1}}}
  ,{{2,0,0,0,0,0,0,0}, {{1,2,0,0,2,0,1,2}, {0,2,2,0,2,1,1}}}
}
run_test()

set_base(10)
name = "uadd"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{2}, {{1}, {1}}}
  ,{{3}, {{2}, {1}}}
  ,{{3}, {{1}, {2}}}
  ,{{1,0}, {{9}, {1}}}
  ,{{1,0}, {{1}, {9}}}

  ,{{1,1}, {{1,0}, {1}}}
  ,{{1,1,0}, {{8,5}, {2,5}}}
  ,{{1,0,0}, {{9,9}, {1}}}
  ,{{1,0,0,0,0,0}, {{9,9,0,9,9}, {9,0,1}}}
  ,{{9,9,9,9,9,9,9,9}, {{1,2,3,4,5,6,7,8}, {8,7,6,5,4,3,2,1}}}

  ,{"1000000000"-'0', {"123456789"-'0', "876543211"-'0'}}
}
run_test()

set_base(2)
name = "udiff"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{0}, {{0}, {0}}}
  ,{{1}, {{0}, {1}}}
  ,{{1}, {{1}, {0}}}
  ,{{1}, {{1,0}, {1}}}
  ,{{1,0}, {{1}, {1,1}}}

  ,{{1,1}, {{1,0,0}, {1}}}
  ,{{1,0,1}, {{1}, {1,1,0}}}
  ,{{1,1}, {{1,0,1}, {1,0,0,0}}}
  ,{{1,0,1,0}, {{1,0,1,0,0}, {1,0,1,0}}}
}
run_test()

set_base(3)
name = "udiff"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{2}, {1}}}
  ,{{2}, {{1,0}, {1}}}
  ,{{2}, {{1}, {1,0}}}
  ,{{1,1,1}, {{1,1,2}, {1}}}
  ,{{1}, {{2,2,2}, {1,0,0,0}}}

  ,{{1,1}, {{1,2}, {1,0,0}}}
  ,{{1,2,0,0,2,0,1,2}, {{2,0,0,0,0,0,0,0}, {0,2,2,0,2,1,1}}}
}
run_test()

set_base(10)
name = "udiff"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{2}, {1}}}
  ,{{1}, {{2}, {3}}}
  ,{{2}, {{3}, {1}}}
  ,{{1}, {{9}, {1,0}}}
  ,{{1}, {{1,0}, {9}}}

  ,{{1,0}, {{1,1}, {1}}}
  ,{{2,5}, {{8,5}, {1,1,0}}}
  ,{{1}, {{9,9}, {1,0,0}}}
  ,{{9,9,0,9,9}, {{1,0,0,0,0,0}, {9,0,1}}}
  ,{{8,7,6,5,4,3,2,1}, {{1,2,3,4,5,6,7,8}, {9,9,9,9,9,9,9,9}}}

  ,{"123456789"-'0', {"1000000000"-'0', "876543211"-'0'}}
}
run_test()

set_base(2)
name = "usub"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{0}, {{0}, {0}}}
  ,{{-1}, {{0}, {1}}}
  ,{{1}, {{1}, {0}}}
  ,{{1}, {{1,0}, {1}}}
  ,{{-1,0}, {{1}, {1,1}}}

  ,{{1,1}, {{1,0,0}, {1}}}
  ,{{-1,0,1}, {{1}, {1,1,0}}}
  ,{{-1,1}, {{1,0,1}, {1,0,0,0}}}
  ,{{1,0,1,0}, {{1,0,1,0,0}, {1,0,1,0}}}
}
run_test()

set_base(3)
name = "usub"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{2}, {1}}}
  ,{{2}, {{1,0}, {1}}}
  ,{{-2}, {{1}, {1,0}}}
  ,{{1,1,1}, {{1,1,2}, {1}}}
  ,{{-1}, {{2,2,2}, {1,0,0,0}}}

  ,{{-1,1}, {{1,2}, {1,0,0}}}
  ,{{1,2,0,0,2,0,1,2}, {{2,0,0,0,0,0,0,0}, {0,2,2,0,2,1,1}}}
}
run_test()

set_base(10)
name = "usub"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{2}, {1}}}
  ,{{-1}, {{2}, {3}}}
  ,{{2}, {{3}, {1}}}
  ,{{-1}, {{9}, {1,0}}}
  ,{{1}, {{1,0}, {9}}}

  ,{{1,0}, {{1,1}, {1}}}
  ,{{-2,5}, {{8,5}, {1,1,0}}}
  ,{{-1}, {{9,9}, {1,0,0}}}
  ,{{9,9,0,9,9}, {{1,0,0,0,0,0}, {9,0,1}}}
  ,{{-8,7,6,5,4,3,2,1}, {{1,2,3,4,5,6,7,8}, {9,9,9,9,9,9,9,9}}}

  ,{"123456789"-'0', {"1000000000"-'0', "876543211"-'0'}}
}
run_test()

set_base(2)
name = "umul"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{0}, {{0}, {0}}}
  ,{{0}, {{0}, {1}}}
  ,{{0}, {{1}, {0}}}
  ,{{1}, {{1}, {1}}}
  ,{{1,0}, {{1,0}, {1}}}

  ,{{1,1}, {{1,1}, {1}}}
  ,{{1,0,0,1}, {{1,1}, {1,1}}}
  ,{{1,1,0,0,0,1}, {{1,1,1}, {1,1,1}}}
  ,{{1,0,1,0}, {{1,0,1}, {1,0}}}
  ,{{1,1,1,1}, {{1,0,1}, {1,1}}}
  ,{{1,1,0,0,1,0,0}, {{1,0,1,0}, {1,0,1,0}}}
}
run_test()

set_base(3)
name = "umul"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{1}, {1}}}
  ,{{2}, {{2}, {1}}}
  ,{{2}, {{1}, {2}}}
  ,{{1,1,1}, {{1,1,1}, {1}}}
  ,{{2,2,2}, {{1,1,1}, {2}}}

  ,{{2,2,2,0}, {{2,2,2}, {1,0}}}
  ,{{1,2,2,1}, {{2,2,2}, {2}}}
  ,{"11200002020002"-'0', {"12002012"-'0', "220211"-'0'}}
}
run_test()

set_base(10)
name = "umul"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{1}, {1}}}
  ,{{2}, {{2}, {1}}}
  ,{{2}, {{1}, {2}}}
  ,{{9}, {{9}, {1}}}
  ,{{9}, {{1}, {9}}}

  ,{{1,0}, {{1,0}, {1}}}
  ,{{2,1,2,5}, {{8,5}, {2,5}}}
  ,{{9,9}, {{9,9}, {1}}}
  ,{{8,9,2,8,8,1,9,9}, {{9,9,0,9,9}, {9,0,1}}}
  ,{"89288199"-'0', {{9,9,0,9,9}, {9,0,1}}}
  ,{"1082152022374638"-'0', {"12345678"-'0', "87654321"-'0'}}
}
run_test()

set_base(2)
name = "udiv"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{0}, {{0}, {1}}}
  ,{{1}, {{1}, {1}}}
  ,{{1}, {{1,0}, {1,0}}}
  ,{{1,0}, {{1,0}, {1}}}
  ,{{1}, {{1,1}, {1,1}}}

  ,{{1,1}, {{1,0,0,1}, {1,1}}}
  ,{{1,1,1}, {"110001"-'0', {1,1,1}}}
  ,{{1,0,1}, {{1,0,1,0}, {1,0}}}
  ,{{1,0,1}, {{1,1,1,1}, {1,1}}}
  ,{{1,1}, {{1,1,1,1}, {1,0,1}}}

  ,{{1,0,1,0}, {"1100100"-'0', {1,0,1,0}}}
}
run_test()

set_base(3)
name = "udiv"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{1}, {1}}}
  ,{{2}, {{2}, {1}}}
  ,{{2}, {{1,1}, {2}}}
  ,{{2,0}, {{1,1,0}, {2}}}
  ,{{1,1,1}, {{2,2,2}, {2}}}

  ,{{2,0,2}, {{1,0,2,0,1}, {1,2}}}
  ,{{2,2,2}, {{1,2,2,1}, {2}}}
  ,{"12002012"-'0', {"11200002020002"-'0', "220211"-'0'}}
}
run_test()

set_base(10)
name = "udiv"
params = "flatseq n1, flatseq n2"
sets = {""
  ,{{1}, {{1}, {1}}}
  ,{{2}, {{2}, {1}}}
  ,{{9}, {{9}, {1}}}
  ,{{1,0}, {{1,0}, {1}}}
  ,{{2,5}, {{2,1,2,5}, {8,5}}}

  ,{{1,9}, {{9,5}, {5}}}
  ,{{9,9,0,9,9}, {{8,9,2,8,8,1,9,9}, {9,0,1}}}
  ,{{9,9,0,9,9}, {"89288199"-'0', {9,0,1}}}
  ,{"12345678"-'0', {"1082152022374638"-'0', "87654321"-'0'}}
}
run_test()
--pause("\n[Done]")

